﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace view
{
    class MemberCommands
    {
        private model.MemberCommands _memberCommands;
        private string exitLetter = "q";

        public MemberCommands()
        {
            _memberCommands = new model.MemberCommands();
        }

        internal model.MemberCommands MemberCommands1
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public void showAllMembersBrief()
        {
            Console.Clear();
            List<model.Member> members = _memberCommands.getAllMembers();
            Console.WriteLine("Kompakt medlemslista:");
            foreach (var member in members)
            {
                Console.WriteLine(String.Format("Namn: {0}, Medlemsnummer: {1}, Antal Båtar: {2}",
                    member.name, member.memberId, member.BoatsList.Count));
            }
        }

        public void showMembersFull(model.Member singleMember = null)
        {
            Console.Clear();
            List<model.Member> members = _memberCommands.getAllMembers();
            if (singleMember == null)
            {
                Console.WriteLine("Fullständig medlemslista:");
                Console.WriteLine("-------------------------");
                Console.WriteLine();
            }
            if (singleMember != null)
            {
                renderMemberDetails(singleMember);
            } else foreach (var member in members)
            {
                renderMemberDetails(member);
            }
        }

        private static void renderMemberDetails(model.Member member)
        {
            Console.WriteLine(String.Format("Namn: {0}, Personnummer: {1}, Medlemsnummer: {2}",
                member.name, member.sSNumber, member.memberId));
            Console.WriteLine("Medlemmens båtar:");
            foreach (var boat in member.BoatsList)
            {
                Console.WriteLine(String.Format("Namn: {0}, Båttyp: {1}, Längd: {2}", boat.boatName, boat.boatType, boat.length));
            }
            Console.WriteLine("-------------------------");
            Console.WriteLine();
        }

        public model.Member getBoatOwner() {
            Console.WriteLine("Välj båtens ägare, eller '" + exitLetter + "' för att avbryta:");
            return getMemberFromDB();
        }

        public model.Member getMember()
        {
            Console.Clear();
            Console.WriteLine("Välj en medlem, eller '" + exitLetter + "' för att avbryta: ");
            return getMemberFromDB();
        }

        private model.Member getMemberFromDB()
        {
            List<model.Member> members = _memberCommands.getAllMembers();
            foreach (var member in members)
            {
                Console.WriteLine(String.Format("Medlemsnummer: {0}, Namn: {1}, Personnummer: {2}",
                    member.memberId, member.name, member.sSNumber));
            }
            string input;
            int output;
            do
            {
                Console.Write("Ange medlemsnummer: ");
                input = Console.ReadLine();
                if (input.ToLower() == exitLetter)
                {
                    throw new FormatException("The user aborted");
                }
                //Kontrollera så att medlemsnumret finns i listan.
                if (int.TryParse(input, out output) && members.Any(x => x.memberId == output))
                {
                    var returnValue = from returnMember in members
                                      where returnMember.memberId == output
                                      select returnMember;
                    return returnValue.Single();
                }
                else
                {
                    Console.WriteLine("FEL! Du måste ange ett giltigt medlemsnummer.");
                }
            } while (true);
        }

        public model.Member getNewMember()
        {
            Console.Clear();
            Console.WriteLine("Skapa ny medlem:");
            model.Member member = new model.Member();
            member.name = getInputString("Ange namn: ");
            member.sSNumber = getInputString("Ange personnummer: ");
            return member;
        }

        public model.Member updateMember(model.Member oldMember)
        {
            Console.Clear();
            Console.WriteLine("Uppdatera medlem:");
            Console.WriteLine("Nuvarande namn: {0} \nNuvarande personnummer: {1}",
                oldMember.name, oldMember.sSNumber);
            string input;
            Console.Write("Ange nytt namn, eller retur för oförändrat: ");
            input = Console.ReadLine();
            if (!String.IsNullOrWhiteSpace(input))
            {
                oldMember.name = input;
            }
            Console.Write("Ange nytt personnummer, eller retur för oförändrat: ");
            input = Console.ReadLine();
            if (!String.IsNullOrWhiteSpace(input))
            {
                oldMember.sSNumber = input;
            }
            return oldMember;
        }

        public void showMemberDetails(model.Member member)
        {
            Console.Clear();
            Console.WriteLine("{0}s detaljer:", member.name);
            showMembersFull(member);
        }

        private string getInputString(string message)
        {
            string input;
            do
            {
                Console.Write(message);
                input = Console.ReadLine();
                if (!String.IsNullOrWhiteSpace(input))
                {
                    return input;
                }
                else
                {
                    Console.WriteLine("FEL! Tom inmatning.");
                }
            } while (true);
        }

        public void removeMemberSuccess()
        {
            Console.Clear();
            Console.WriteLine("Medlemmen och dess båtar togs bort.");
        }

        public void addMemberSuccess()
        {
            Console.Clear();
            Console.WriteLine("Medlemmen lades till");
        }

        public void updatedMemberSuccess()
        {
            Console.Clear();
            Console.WriteLine("Medlemmen uppdaterades");
        }

        public void commandAborted()
        {
            Console.Clear();
            Console.WriteLine("Åtgärden avbruten");
        }

    }
}
